package io.renren.modules.collecting.service.impl;

import io.renren.modules.collecting.dao.CollectingDeviceDao;
import io.renren.modules.collecting.entity.CollectingDeviceEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.collecting.dao.CollectingSpaceDao;
import io.renren.modules.collecting.entity.CollectingSpaceEntity;
import io.renren.modules.collecting.service.CollectingSpaceService;

import javax.annotation.Resource;


@Service("collectingSpaceService")
public class CollectingSpaceServiceImpl extends ServiceImpl<CollectingSpaceDao, CollectingSpaceEntity> implements CollectingSpaceService {

    @Resource
    private CollectingSpaceDao collectingSpaceDao;
    @Resource
    private CollectingDeviceDao collectingDeviceDao;

    //空间列表
    @Override
    public PageUtils getList(Map<String, Object> params) {
        String name = null;
        if (null != params.get("name") && !"".equals(params.get("name"))) {
            name = String.valueOf(params.get("name"));
        }
        IPage<CollectingSpaceEntity> page = collectingSpaceDao.getList(new Query<CollectingSpaceEntity>().getPage(params), name);
        List<CollectingSpaceEntity> records = page.getRecords();
        for (CollectingSpaceEntity space : records) {
            space.setDeviceList(collectingDeviceDao.getDeviceList(space.getId()));
        }
        return new PageUtils(page);
    }

    //空间详情
    @Override
    public CollectingSpaceEntity get(Integer id) {
        CollectingSpaceEntity collectingSpace = this.getById(id);
        collectingSpace.setDeviceList(collectingDeviceDao.getDeviceList(id));
        return collectingSpace;
    }

    //新增
    @Override
    public void add(CollectingSpaceEntity collectingSpace) {
        collectingSpace.setModifyTime(new Date());
        collectingSpaceDao.insert(collectingSpace);
        if (null != collectingSpace.getDeviceList() && collectingSpace.getDeviceList().size() > 0) {
            //修改绑定id
            for (CollectingDeviceEntity device : collectingSpace.getDeviceList()) {
                device.setSpaceBind(collectingSpace.getId());//获取采集空间的主键
                collectingDeviceDao.updateById(device);
            }
        }

    }

    //修改
    @Override
    public void update(CollectingSpaceEntity collectingSpace) {
        collectingSpace.setModifyTime(new Date());
        collectingSpaceDao.updateById(collectingSpace);
        //清空绑定id
        collectingDeviceDao.updateSpaceBind(collectingSpace.getId());

        if (null != collectingSpace.getDeviceList() && collectingSpace.getDeviceList().size() > 0) {
            ////修改绑定id
            for (CollectingDeviceEntity device : collectingSpace.getDeviceList()) {
                device.setSpaceBind(collectingSpace.getId());//获取采集空间的主键
                collectingDeviceDao.updateById(device);
            }
        }

    }

}